/**
 * Created by Administrator on 2017/6/29 0029.
 */
"use strict";
angular.module('app.sensor').controller('DataCtrl', function ($scope,$http,$interval) {
    // 获取该端口的字段名称
      $scope.fields = {};
      // $http.get('/list_nopage/device_parameters?type==4&pro_type==8053').then(function(res){
      //   // console.log(res.data);
      //   if (res.data.ret == 1) {
      //     var arr = res.data.info;   
      //     for (var i = 0; i < arr.length; i++) {
      //       var obj = arr[i];
      //       $scope.fields[obj.key1] = obj.value1;  
      //     }
      //      // console.log($scope.fields);
      //   }
      // }) 
      $http.get(baseurl+'/table_columns/log_8053').then(function(res){
             //console.log(res.data.info);
            if (res.data.ret == 1) {
                for(var i = 0 ; i < res.data.info.length; i++){
                    var obj = res.data.info[i];                    
                    if(obj.COLUMN_NAME === 'id'){
                        obj.column_comment = '编号';
                    }else if(obj.COLUMN_NAME === 'deviceid'){
                        obj.column_comment = '设备编号';
                    }else if(obj.COLUMN_NAME === 'fid'){
                        obj.column_comment = '公司';
                    }else if(obj.COLUMN_NAME === 'projectid'){
                        obj.column_comment = '项目';
                    }
                    $scope.fields[obj.COLUMN_NAME] = obj.column_comment;
                }
                sends();
            }
      });
    //设备分组
      $http.get(baseurl + "list_nopage/group1").then(function (res) {
          console.log(res.data);
          $scope.deviceList = res.data.info;
      })
    // 实时数据
    //根据projectid获取设备
      $scope.getDevice = function (projectid) {
          console.log(projectid);
          $http.get(baseurl + '/list_nopage/device?projectid='+projectid).then(function (res) {
              console.log(res.data);
          });
      }
    //配置分页基本参数
    var sends = function () {
       $http.get(baseurl+'/actual_data/8053').then(function(res){
            //console.log(res.data.info);
           // alert(JSON.stringify(res.data.info[0]));
            $scope.DATAS = [];
            
           if(res.data.info.length > 0){
               for(var i = 0; i < res.data.info.length; i++){
                   for (var key in res.data.info[i]) {
                       $scope.deviceid = res.data.info[i].deviceid;
                       if(key !== 'deviceid'){
                           var obj = {};
                           obj.id = res.data.info[i]['deviceid'];
                           if ($scope.fields[key]) {
                             obj.name = $scope.fields[key];
                           }else{
                             obj.name = key;
                           }
                           obj.data = res.data.info[i][key];
                           obj.data = obj.data.toFixed(2);
                           $scope.DATAS.push(obj);
                       }
                   }
               }
           }
          if (res.data.ret == 1) {
            $scope.realdata = res.data.info;
            $('.blink').css('color','black');  //默认值
            setTimeout(" $('.blink').css('color','blue')",200); //第一次闪烁
            setTimeout( "$('.blink').css('color','black')",1000); //第二次闪烁
          }
        })
    }
    
     var timeout = $interval(sends, 10000);
     $scope.$on('$destroy', function () {
         $interval.cancel(timeout);
     })
      // 模拟传感器发假数据
      $http.get(baseurl+'/select_deviceid?port==8053').then(function(res){
         if (res.data.ret == 1) {
            $scope.deviceids = res.data.info;
         }
      })
      //$scope.min = {};
      //$scope.sensor_data = function(){
      //   if ($scope.deviceid == '') {
      //     alert("请输入模拟设备id");
      //     return;
      //   }
      //   var data = {
      //     data:$scope.min,
      //     data_ext:{
      //        // press_unit:'pa',
      //        deviceid:$scope.min.deviceid

      //     }
      //   }
      //   $http.post(baseurl+'/sensor_data_public2/8053',data).then(function(res){
      //      // console.log(res.data);
      //      if (res.data.ret == 1) {
      //         new Notify('success','发送成功');
      //      }
      //   })
      //}
      // 测试
      // $http.get(baseurl+'/history_data_yk?deviceid==00010001&time=2017-12-28,2018-01-02').then(function(res){
      //     console.log(res.data);
      // });
});
